home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / scan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  1.9 KB  |  103 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4. /* Copyright Herve' Touati, Aquarius Project, UC Berkeley */
  5.  
  6. #include <stream.h>
  7. #include <ctype.h>
  8. #include "hash_table.h"
  9. #include "string_table.h"
  10. #include "scan.h"
  11.  
  12. void Scan::place_pointers()
  13. {
  14.   static const char quote_char = '\'';
  15.   register char* head = input_buffer;
  16.   char* q[4];
  17.   int quotes;
  18.   for (int i = 0; i < 4; i++) {
  19.     quotes = 0;
  20.     while (isspace(*head))
  21.       head++;
  22.     if (*head == ',')
  23.       head++;
  24.     if (*head == quote_char) {
  25.       quotes = 1;
  26.       head++;
  27.     }
  28.     p[i] = head;
  29.     if (quotes == 1) {
  30.       while (*head != quote_char && *head != '\0') 
  31.     head++;
  32.       q[i] = head;
  33.       if (*head == quote_char)
  34.     head++;
  35.     } else {
  36.       while (*head != ',' && !isspace(*head) && *head != '\0')
  37.     head++;
  38.       q[i] = head;
  39.     }
  40.   }
  41.   for (i = 0; i < 4; i++) 
  42.     *q[i] = '\0';
  43. }
  44.  
  45. void Scan::next_line()
  46. {
  47.   char nl;
  48.   for (;;) {
  49.     cin.get(input_buffer, buffer_size,'\n');
  50.     cin.get(nl);
  51.     if (cin.rdstate() != _good) {
  52.       status = SCAN_EOF;
  53.       return;
  54.     }
  55.     place_pointers();
  56.     if (*p[0] != '\0')
  57.       break;
  58.   }
  59.   if (*p[0] == '_') {
  60.     status = SCAN_DEF_LABEL;
  61.     return;
  62.   }
  63.   intern_p0 = intern(p[0]);
  64.   if (intern_p0 == end || *p[0] == '[') { /* last message of compiler "[... */
  65.     status = SCAN_EOF;
  66.     return;
  67.   }
  68.   if (intern_p0 == procedure) {
  69.     status = SCAN_DEF_PROC;
  70.     return;
  71.   }
  72.   status = SCAN_INSTR;
  73. }
  74.  
  75. void Scan::print_state()
  76. {
  77.   cout << " status = " << status << "\t";
  78.   for (int i = 0; i < 4; i++)
  79.     cout << "\"" << p[i] << "\" ";
  80.   cout << "\n";
  81. }
  82.  
  83. #ifdef DEBUG_SCAN
  84. void out_of_store()
  85. {
  86.   cerr << "operator new failed: out of store\n";
  87.   exit(1);
  88. }
  89. typedef void (*PF)();
  90. extern PF set_new_handler(PF);
  91.  
  92. main() {
  93.   Scan S;
  94.   set_new_handler(&out_of_store);
  95.   cout << "Try out a scanner \n";
  96.   while (S.status != SCAN_EOF) {
  97.     S.next_line();
  98.     S.print_state();
  99.   }
  100.   S.print();
  101. }
  102. #endif
  103.